package com.hengtiansoft.lfy.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.been.Child;
import com.hengtiansoft.lfy.been.ChildType;
import com.hengtiansoft.lfy.db.CL_InviteMessgeDao;
import com.hengtiansoft.lfy.entity.CategroyEntity;
import com.hengtiansoft.lfy.entity.ServiceResult;
import com.hengtiansoft.lfy.fragment.Change_ChineseEnglishFragment;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.widget.PagerSlidingTabStrip;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 更改场景详情页面
 */
public class Change_SceneDetailsActivity extends BaseActivity {
    private ViewPager mViewPager;
    private MyAdapter mAdapter;
    private List<Fragment> mDatas;
    private ImageView mIvBack;
    private TextView mTvMessageTitle;
    private Intent fromWhere;
    private String from;
    private Integer childId;
    private Integer categroyId;
    private String strWords;
    private PagerSlidingTabStrip tabs;
    private List<ChildType> childTypes=new ArrayList<ChildType>();
  //  private ChangeSQLiteDom cd;
    private String isneedUpdate="0";
    private CL_InviteMessgeDao cl_inviteMessgeDao;
    private String TAG="Change_SceneDetailsActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.change_activity_scene_details);
        initTabLine();
        initView();
    }

    private void initTabLine() {

    }

    private void initView() {
     //   cd = new ChangeSQLiteDom();
        cl_inviteMessgeDao=new CL_InviteMessgeDao(getBaseContext());
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        mTvMessageTitle = (TextView) findViewById(R.id.message_title);
        fromWhere = getIntent();
        from = fromWhere.getStringExtra("from");
        categroyId = fromWhere.getIntExtra("categroyId", 0);
        strWords = fromWhere.getStringExtra("strWords");
        childId = fromWhere.getIntExtra("childId", 0);
        isneedUpdate=fromWhere.getStringExtra("isneedUpdate");
        String title = fromWhere.getStringExtra("title");
        mTvMessageTitle.setText("场景用语");
        getIntent().putExtra("from", from);
        getIntent().putExtra("categroyId", categroyId);
        getIntent().putExtra("strWords", strWords);
        getIntent().putExtra("childId", childId);
        getIntent().putExtra("title", title);
        getIntent().putExtra("isneedUpdate",isneedUpdate);
        mDatas = new ArrayList<Fragment>();
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //此处要处理，没有数据时程序会挂
       // childTypes=cd.getChildType(childId); //
        if("1".equals(isneedUpdate)) {
            getchild_TypeCategroyFromServer(childId+"",getBaseContext());
        }else{
            childTypes=cl_inviteMessgeDao.getChildType(childId+"");
            if(childTypes.size()==0){
                Toast.makeText(getBaseContext(),getString(R.string.not_subclassification), Toast.LENGTH_LONG).show();
                finish();
            }
            mAdapter=new MyAdapter(getSupportFragmentManager(),childTypes);
            mViewPager.setAdapter(mAdapter);
            tabs.setViewPager(mViewPager);
        }

    }
    //网络请求
    public void getchild_TypeCategroyFromServer(final String childId, final Context mContext){//GetChildCategroyJson getChildCategroyJson
        NetHelper.getchild_TypeCategroyFromServer(mContext,childId, new NetUtils.NetCallBack<ServiceResult>() {
                    @Override
                    public void success(ServiceResult rspData) {
                        CategroyEntity info = (CategroyEntity) rspData;
                        int code = info.getRetcode();
                        List<Child> data = info.getData();
                        String message = info.getMessage();
                        if (code == 0) {
                            if (null != data) {
                                childTypes.clear();
                                cl_inviteMessgeDao.upDateChildCategroy_isNeed_off(childId);
                                Intent intent = new Intent("com.lfy.update");
                                sendBroadcast(intent);
                                for(Child child:data){
                                    ChildType childType=new ChildType();
                                    childType.setChild_fen_detail(child.getName());
                                    childType.setChild_fen_e_detail(child.getName());
                                    childType.setChild_id(childId);
                                    childType.setIsneedUpdate("1");
                                    childType.setChild_fen_id(child.getId() + "");
                                    childTypes.add(childType);
                                }
                                if(childTypes.size()==0){
                                    Toast.makeText(getBaseContext(),getString(R.string.not_subclassification), Toast.LENGTH_LONG).show();
                                    finish();
                                    return;
                                }
                                cl_inviteMessgeDao.saveChildType(childTypes,childId);
                                mAdapter=new MyAdapter(getSupportFragmentManager(),childTypes);
                                mViewPager.setAdapter(mAdapter);
                                tabs.setViewPager(mViewPager);
                            } else {
                                Toast.makeText(mContext, getString(R.string.get_scene_error),
                                        Toast.LENGTH_LONG).show();
                            }
                        }else {
                            Toast.makeText(mContext, message, Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
                    @Override
                    public void failed(String msg) {
                        Toast.makeText(mContext, msg,
                                Toast.LENGTH_LONG).show();

                    }
                }, CategroyEntity.class);
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    public class MyAdapter extends FragmentStatePagerAdapter {
        List<ChildType> _childTypeList;
        public MyAdapter(FragmentManager fm, List<ChildType> childTypeList) {
            super(fm);
            _childTypeList=childTypeList;
        }

       @Override
        public CharSequence getPageTitle(int position) {
            return _childTypeList.get(position).getChild_fen_detail();
        }

        @Override
        public int getCount() {
            return _childTypeList.size();
        }

        @Override
        public Fragment getItem(int position) {
            Change_ChineseEnglishFragment chineseEnglishFragment= Change_ChineseEnglishFragment.newInstance(_childTypeList.get(position).getChild_id(),_childTypeList.get(position).getChild_fen_id(),_childTypeList.get(position).getIsneedUpdate());
            return chineseEnglishFragment;

        }
    }
}
